package cn.myfirefly.moming.portal.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.myfirefly.moming.common.model.MmSystemConfigDto;
import cn.myfirefly.moming.common.model.MmSystemConfigFilterDto;
import cn.myfirefly.moming.common.service.SystemConfigService;
import cn.myfirefly.moming.portal.entity.MMSystemAccess;
import cn.myfirefly.moming.portal.service.SystemAccessService;
import cn.myfirefly.core.BaseController;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.ResultDto;

@Controller
@RequestMapping("systemConfig")
public class SystemConfigController extends BaseController {

	@Autowired
	private SystemConfigService systemConfigService;
	
	@Autowired
	private SystemAccessService systemAccessService;
	
	@RequestMapping("index.do")
	public String index(HttpServletRequest request, 
			HttpServletResponse response) {
		
		return "systemconfig/index";
	}
	
	@RequestMapping("syslist.do")
	@ResponseBody
	public List<Map<String, String>> syslist(HttpServletRequest request, 
			HttpServletResponse response) {
		List<MMSystemAccess> list = systemAccessService.getSystemAccessList(true);
		List<Map<String, String>> items = new ArrayList<>();
		for (MMSystemAccess mmSystemAccess : list) {
			Map<String, String> map = new HashMap<>();
			map.put(mmSystemAccess.getSystemId().toString(), mmSystemAccess.getSystemName());
			items.add(map);
		}
		return items;
	}
	
	@RequestMapping("edit.do")
	public String edit(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("syslist", systemAccessService.getSystemAccessList(true));
		return "systemconfig/edit";
	}
	
	@RequestMapping("add.do")
	public String add(HttpServletRequest request, 
			HttpServletResponse response) {
		request.setAttribute("syslist", systemAccessService.getSystemAccessList(true));
		return "systemconfig/add";
	}
	
	@RequestMapping("addSave.do")
	@ResponseBody
	public ResultDto addSave(MmSystemConfigDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() != null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request, true);
		int ret = systemConfigService.insert(dto);
		if (ret == -1) {
			return error("配置已存在，请重新输入");
		}
		return success("保存成功");
	}
	
	@RequestMapping("editSave.do")
	@ResponseBody
	public ResultDto editSave(MmSystemConfigDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		if (dto.getId() == null) {
			return error("请求参数错误");
		}
		warpModifyInfoByName(dto, request);
		systemConfigService.update(dto);
		return success("保存成功");
	}
	
	@SuppressWarnings("rawtypes")
	@RequestMapping("list.do")
	@ResponseBody
	@BjuiDataGridFilterDtoWrap
	public PageResultDto list(MmSystemConfigFilterDto dto) {
		PageResultDto result = systemConfigService.getList(dto);
		return result;
	}
	
	@RequestMapping("get.do")
	@ResponseBody
	public ResultDto getMmSystemConfig(MmSystemConfigDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		MmSystemConfigDto mmSystemConfig = systemConfigService.getMmSystemConfig(dto.getId());
		return success(mmSystemConfig);
	}
	
	@RequestMapping("del.do")
	@ResponseBody
	public ResultDto del(MmSystemConfigDto dto, HttpServletRequest request, 
			HttpServletResponse response) {
		String msg = systemConfigService.del(dto.getId());
		if (!Const.ResultCode.SUCCESS.equals(msg)) {
			return error(msg);
		}
		return success("删除成功");
	}
}
